package jinyilw.swing.visuals.Rcomp;

import jinyilw.swing.SwingTools;
import jinyilw.swing.i18n.IString;
import jinyilw.swing.i18n.LanguageTools;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.Locale;

public class RColorChooser extends JColorChooser
{
	private static final long serialVersionUID = -2165663325065827098L;
	private static RColorChooser defaultChooser;
	private static String languageTag;
	private CChooserDialog dialog;
	private ColorTracker colorTracker;

	public RColorChooser(Component com, String title, Color initialColor)
	{
		super(initialColor != null ? initialColor : Color.white);
		Window window = SwingTools.getWindowForComponent(com);
		dialog = new CChooserDialog(window, title,
				JDialog.DEFAULT_MODALITY_TYPE);
		// dialog.setSize(630, 430);
		dialog.pack();
		dialog.setResizable(false);
	}

	public Color showDialog(Component com)
	{
		dialog.setLocationRelativeTo(com);
		dialog.setVisible(true);
		return colorTracker.getColor();
	}

	public Color getChooseColor()
	{
		return colorTracker.getColor();
	}

	public void setDialogTitle(String title)
	{
		dialog.setTitle(title);
	}

	public static Color showDefaultDialog(Component com, String title,
			Color initialColor)
	{
		if (defaultChooser == null)
			defaultChooser = new RColorChooser(com, null, initialColor);
		defaultChooser.setDialogTitle(title);
		return defaultChooser.showDialog(com);
	}

	public static Color showDefaultDialog(Component com, IString iTitle,
			Color initialColor)
	{
		if (defaultChooser == null || languageTag != LanguageTools.languageTag)
		{
			if (defaultChooser != null)
				initialColor = defaultChooser.getChooseColor();
			defaultChooser = new RColorChooser(com, null, initialColor);
			languageTag = LanguageTools.languageTag;
		}
		if (iTitle != null)
			defaultChooser.setDialogTitle(iTitle.getI18nString());
		return defaultChooser.showDialog(com);
	}

	class CChooserDialog extends JDialog
	{
		private static final long serialVersionUID = 5074516862216684199L;
		private Color initialColor;
		private JColorChooser chooserPane;
		private final Component component;

		public CChooserDialog(Window window, String title,
				ModalityType modalityType)
		{
			super(window, title, modalityType);
			initColorChooserDialog();
			component = window;
			if (title != null)
				getAccessibleContext().setAccessibleDescription(title);
		}

		protected void initColorChooserDialog()
		{
			chooserPane = RColorChooser.this;
			Locale locale = getLocale();
			String okString = UIManager.getString("ColorChooser.okText",
					locale);
			String cancelString = UIManager.getString("ColorChooser.cancelText",
					locale);
			String resetString = UIManager.getString("ColorChooser.resetText",
					locale);

			Container contentPane = getContentPane();
			contentPane.setLayout(new BorderLayout());
			contentPane.add(chooserPane, BorderLayout.CENTER);

			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
			JButton okButton = new JButton(okString);
			getRootPane().setDefaultButton(okButton);
			okButton.getAccessibleContext().setAccessibleDescription(okString);
			okButton.setActionCommand("OK");
			okButton.addActionListener(e -> dispose());
			colorTracker = new ColorTracker(chooserPane);
			okButton.addActionListener(colorTracker);
			buttonPane.add(okButton);

			JButton cancelButton = new JButton(cancelString);
			cancelButton.getAccessibleContext()
					.setAccessibleDescription(cancelString);
			cancelButton.setActionCommand("cancel");
			cancelButton.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent e)
				{
					colorTracker.setColor(null);
					dispose();
				}
			});
			buttonPane.add(cancelButton);

			JButton resetButton = new JButton(resetString);
			resetButton.getAccessibleContext()
					.setAccessibleDescription(resetString);
			resetButton.addActionListener(e -> chooserPane.setColor(initialColor));
			buttonPane.add(resetButton);
			contentPane.add(buttonPane, BorderLayout.SOUTH);
			if (JDialog.isDefaultLookAndFeelDecorated() && UIManager
					.getLookAndFeel().getSupportsWindowDecorations())
			{
				getRootPane().setWindowDecorationStyle(
						JRootPane.COLOR_CHOOSER_DIALOG);
			}
			applyComponentOrientation(
					((component == null) ? getRootPane() : component)
							.getComponentOrientation());

			// setAlwaysOnTop(true);
		}

		@Override
		public void setVisible(boolean b)
		{
			initialColor = chooserPane.getColor();
			super.setVisible(b);
		}
	}

	static class ColorTracker implements ActionListener, Serializable
	{
		private static final long serialVersionUID = 525682485870888946L;
		JColorChooser chooser;
		Color color;

		public ColorTracker(JColorChooser c)
		{
			chooser = c;
		}

		@Override
		public void actionPerformed(ActionEvent e)
		{
			color = chooser.getColor();
		}

		public void setColor(Color c)
		{
			color = c;
		}

		public Color getColor()
		{
			return color;
		}
	}
}